package com.compass.doctor.base

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.compass.framework.base.BaseFragment
import com.compass.framework.ext.saveAs
import com.compass.framework.ext.saveAsUnChecked
import java.lang.reflect.ParameterizedType

/**
 * 只封装ViewBinding  VM使用KTX by实现
 * @desc   BaseVBFragment
 */
abstract class BaseVBFragment<VB : ViewBinding> : BaseFragment() {
    var mBinding: VB? = null

    override fun getContentView(inflater: LayoutInflater, container: ViewGroup?): View {
        val type = javaClass.genericSuperclass
        val vbClass: Class<VB> = type!!.saveAs<ParameterizedType>().actualTypeArguments[0].saveAs()
        val method = vbClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
        mBinding = method.invoke(this, layoutInflater)!!.saveAsUnChecked()
        return mBinding!!.root
    }

    override fun getLayoutResId(): Int = 0

    override fun onDestroyView() {
        super.onDestroyView()
        mBinding = null
    }
}